![]() |
PATH![]() |
The Mac OS USB Service Library (USL) allows class drivers to poll for the completion of USL function calls by polling the usbRefcon field of the parameter block. In general, it is strongly advised to use asynchronous completion via the call back mechanism defined in Asynchronous Call Support instead of polling. Polling the usbRefcon field is discouraged and should only be implemented under exceptional circumstances.
The primary concern with polling lies with code that is designed to use the asynchronous USL call mechanism, and then enters a tight code loop where little happens except to check the usbRefcon field, and only exits the loop when the usbRefcon field changes. This form of polling robs time from the system, because nothing useful can happen while the code runs in the tight loop.
USB time scales are on the order of milliseconds. A tight polling loop represents and eternity of time wasted, when the system could be doing useful work. Some devices have very slow completion times. Completion times on the order of 100ms are not uncommon. If a driver polled for this length of time, the user would notice the pause, and system performance could suffer. In fact, there are circumstances in which polling the parameter block can cause the system to hang. These circumstances and some guidelines for avoiding them are further defined below:
Never poll from secondary interrupt time. Secondary interrupts are queued, and most I/O including the USL completes at secondary interrupt time. If you poll within secondary interrupt time, USL calls will never get a chance to complete, and the poll will never complete.
If you poll from task time the system may still hang. In order to guard against this you should either:
1). Give time back to the system by calling waitnext event.
2). Only poll for a limited time.
Option 1 is usually only practical from an application. Applications should not be making USL calls. Only class drivers should make USL calls. The use of USL calls by an application, is not supported.
Option 2 can be used by class drivers. The USB standard calls for a 5 second timeout on all transactions. However, this is not currently implemented by the USL. The polling software should make frequent calls to USBGetFrameNumberImmediate to determine the elapsed time. If the elapsed time becomes too great, the attempt should be abandoned with the USBAbortPipeByReference call.
In general, it is best to use asynchronous completion routines wherever possible.
Previous | Back Up One Level | Next |